﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Catalyst
{
	public class ParamValueCollection : IDictionary<string, string>
	{
		private Dictionary<string, string> items = new Dictionary<string, string>();

		public string ParamValueString
		{
			get
			{
				string returnValue = "";

				int i = 0;

				foreach (KeyValuePair<string, string> item in items)
				{
					returnValue += String.Format("{0}={1}", item.Key, item.Value);

					if (i < items.Count - 1)
					{
						returnValue += "&";
					}

					i++;
				}

				return returnValue;
			}
			private set
			{
				string[] keyValues = value.Split('&');

				string key = null;
				string val = null;
				int firstEqualsIndex;
				foreach (string keyVal in keyValues)
				{
					if (keyVal.Contains("="))
					{
						firstEqualsIndex = keyVal.IndexOf("=");

						key = keyVal.Substring(0, firstEqualsIndex);
						val = keyVal.Substring(firstEqualsIndex + 1);

						Add(key, val);
					}
				}
			}
		}

		public ParamValueCollection(string paramValueString = null)
		{
			if (!String.IsNullOrEmpty(paramValueString))
			{
				ParamValueString = paramValueString;
			}
		}


		#region IDictionary<string,string> Members

		public void Add(string key, string value)
		{
			try
			{
				items.Add(key.ToLower(), value);
			}
			catch (ArgumentException ex)
			{
				//throw new Exception(String.Format("An item with key \"{0}\" has already been added.", key));

				this[key] += "," + value;
			}
		}

		public bool ContainsKey(string key)
		{
			return items.ContainsKey(key.ToLower());
		}

		public ICollection<string> Keys
		{
			get { return items.Keys; }
		}

		public bool Remove(string key)
		{
			return items.Remove(key.ToLower());
		}

		public bool TryGetValue(string key, out string value)
		{
			return items.TryGetValue(key.ToLower(), out value);
		}

		public ICollection<string> Values
		{
			get { return items.Values; }
		}

		public string this[string key]
		{
			get
			{
				try
				{
					return items[key.ToLower()];
				}
				catch (KeyNotFoundException ex)
				{
					throw new Exception(String.Format("Key \"{0}\" does not exist in collection.", key));
				}
			}
			set
			{
				try
				{
					items[key.ToLower()] = value;
				}
				catch (ArgumentException ex)
				{
					throw new Exception(String.Format("Key \"{0}\" does not exist in collection.", key));
				}
			}
		}

		#endregion

		#region ICollection<KeyValuePair<string,string>> Members

		public void Add(KeyValuePair<string, string> item)
		{
			Add(item.Key.ToLower(), item.Value);
		}

		public void Clear()
		{
			items.Clear();
		}

		public bool Contains(KeyValuePair<string, string> item)
		{
			foreach (var pair in items)
			{
				if (pair.Key == item.Key.ToLower() && pair.Value == item.Value)
				{
					return true;
				}
			}

			return false;
		}

		public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
		{
			throw new NotImplementedException();
		}

		public int Count
		{
			get { return items.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(KeyValuePair<string, string> item)
		{
			try
			{
				return Remove(item.Key);
			}
			catch (KeyNotFoundException ex)
			{
				return false;
			}
		}

		#endregion

		#region IEnumerable<KeyValuePair<string,string>> Members

		public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
		{
			return items.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return items.GetEnumerator();
		}

		#endregion
	}
}
